package com.zrt.pad.util;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.bertlv.MisComm;
import com.zrt.pad.commonui.SharedApplication;
import com.mobilepos.BluetoothConfig;
import com.mobilepos.MainApp;
import com.mobilepos.MisDataCenter;
import com.mobilepos.MisDataResult;
import com.mobilepos.NonVolatile;
import com.pax.mposapi.ConfigManager;

public class PosUtil {
	
	private Context context = SharedApplication.context;

	private boolean hasPreferDevice = false;
	private final String NULL_MAC = "00:00:00:00:00:00";
	public static final String ACTION_PAIRED = "android.bluetooth.device.action.PAIRING_REQUEST";
	public PosUtil(){
		BluetoothConfig.cfm = ConfigManager.getInstance(context);
		BluetoothConfig.cfm.commType = "bluetooth";
		BluetoothConfig.cfm.save();
		BluetoothConfig.nv = NonVolatile.getInstance(context);
		BluetoothConfig.nv.load();
		hasPreferDevice = !BluetoothConfig.cfm.bluetoothMac.equals(NULL_MAC);
		IntentFilter filter = new IntentFilter();
		filter.addAction(BluetoothDevice.ACTION_FOUND);
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
		filter.addAction(ACTION_PAIRED);
		SharedApplication.context.registerReceiver(receiver, filter);
	}
	
	@Override
	protected void finalize() throws Throwable {
		SharedApplication.context.unregisterReceiver(receiver);
		super.finalize();
	}
	
	public boolean tryConnect(){
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		if (!adapter.isEnabled()) {
			adapter.enable();
		}
		MainApp m = (MainApp) context;
		if (hasPreferDevice) {
			m.m_strMAC = BluetoothConfig.cfm.bluetoothMac;
			byte[] data = MisComm.getConnectArray().getEncoder();
			MisDataResult result = MisDataCenter.dataSwitch(context, data);
			if (result.bSuccess) {
				return true;
			}
		}
		//ËŃË÷ŔśŃŔ
		synchronized (lock) {
			adapter.startDiscovery();
			try {
				lock.wait(20000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return hasPreferDevice;
	}
	private Object lock = new Object();
	private BroadcastReceiver receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.d("bt", action);
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				MainApp m = (MainApp) context;
				m.m_strMAC = device.getAddress();
				byte[] data = MisComm.getConnectArray().getEncoder();
				MisDataResult result = MisDataCenter.dataSwitch(context, data);
				if (result.bSuccess) {
					BluetoothConfig.cfm.bluetoothMac = m.m_strMAC;
					BluetoothConfig.cfm.save();
					hasPreferDevice = true;
					BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
				}
				return;
			}
			if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
				synchronized (lock) {
					lock.notify();
				}
				return;
			}
			if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
				Log.d("bt", "state changed");
				return;
			}
			if (ACTION_PAIRED.equals(action)) {
				Log.d("bt", "paired request");
				return;
			}
		}
	};
	
	public void OnLineResponse(){
//		byte info = MisComm.getOnLineResponseArray(originalInterface, Count)
	}
	
	public boolean connect(){
		byte[] data = MisComm.getConnectArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean downloadMainKey(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getDownLoadMainKeyArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean downloadParameters(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getDownLoadParaArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean login(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getLoginArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean consume(String money){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getConsumeArray(money).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean consumeRedo(String id){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getConsumeRedoArray(id).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean leftMoney(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getLeftMoneyArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean eCardMoney(String money){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getEarMarkArray(money).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean eCardLeftMoney(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getECardLeftMoneyArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean returnGoods(String money){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getRetainGoodsArray(money).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	/**
	 * ˝áËă
	 * @return
	 */
	public boolean account(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getECardLeftMoneyArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean transPayment(String money){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getTransPaymentArray(money).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean vertify(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getVertifyArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean offlineTradeCommit(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getOffLineTradeCommitArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean readTradeDetails(String id){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getReadTradeDetailsArray(id).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean readFailureTradeDetails(String id){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getReadFailureTradeDetailsArray(id).getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}
	
	public boolean clearTradeDetail(){
		if (!tryConnect()) {
			return false;
		}
		byte[] data = MisComm.getClearTradeDetailArray().getEncoder();
		MisDataResult result = MisDataCenter.dataSwitch(context, data);
		return result.bSuccess;
	}


	public String getVersion() {
		return "1.0";
	}
}
